package com.stiove.whwh.Controller;

import com.alibaba.fastjson.JSONObject;
import com.stiove.whwh.dao.pojo.User;
import com.stiove.whwh.service.OrderService;
import com.stiove.whwh.service.TicketService;
import com.stiove.whwh.service.UserService;
import com.stiove.whwh.vo.Result;
import com.stiove.whwh.vo.We;
import com.stiove.whwh.vo.Wq;
import com.stiove.whwh.vo.Ww;
import com.stiove.whwh.vo.params.OrderParams;
import com.stiove.whwh.vo.params.UserLoginParams;
import com.stiove.whwh.vo.params.WxInfoParams;
import com.stiove.whwh.vo.params.WxUserLoginParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private TicketService ticketService;
    @Autowired
    private OrderService orderService;
    @PostMapping("/login")
    public Result login(@ModelAttribute UserLoginParams user){
        return userService.userLogin(user.getPhone(),user.getSmsCode());
    }

    @GetMapping("/sms/{phone}")
    public Result sms(@PathVariable("phone") String phone){
        return userService.send_message(phone);
    }

    @PostMapping("/register")
    public Result register(@ModelAttribute UserLoginParams user){
        return userService.userRegister(user);
    }

    @PostMapping("/headImg")
    public Result updateHeadImg(@RequestPart("img") MultipartFile img) throws IOException {
        return userService.updateHeadImg(img);
    }

    @PutMapping("/username")
    public Result updateUsername(@RequestParam("username") String username){
        return userService.updateUsername(username);
    }

    @PostMapping("/order")
    public Result creatOrder(@ModelAttribute OrderParams orderParams){
        return orderService.creatOrder(orderParams);
    }

    @GetMapping("/userinfo")
    public Result userInfo(){
        return userService.userInfo();
    }

    @PostMapping("/wx/login")
    public Result WxLogin(@ModelAttribute WxUserLoginParams user){
        return userService.wxUserLogin(user);
    }


    @GetMapping("/ww/{page}")
    public Object ww(@PathVariable("page") String page){
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8));
        String uri = "http://154.23.134.68:8080/renren-fast/images/images/listpage";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8"));

//        JSONObject params = JSONObject.parseObject();

        HttpEntity<String> entity = new HttpEntity<String>("{\n" +
                "  \"page\": "+ page + ",\n" +
                "  \"limit\": \"500\",\n" +
                "  \"orderbyType\": \"salenum\"\n" +
                "}",headers);
        ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.POST, entity, String.class);

        Wq parse = JSONObject.parseObject(response.getBody(), Wq.class);
//        return response.getBody();
        ArrayList<Object> arrayList = new ArrayList<>();
        for (We we : parse.getPage().getList()) {
            arrayList.add(we.getId());
        }
        return arrayList;
    }

}
